\documentclass{article}\usepackage[]{graphicx}\usepackage[]{color}
%% maxwidth is the original width if it is less than linewidth
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%

\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother

\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}

\input{../labStyle}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}

\begin{document}

\section*{Lab 3: Distributions of random variables} % Am I Normal?

\license{This is a product of OpenIntro that is released under a Creative Commons Attribution-ShareAlike 3.0 Unported (\web{http://creativecommons.org/licenses/by-sa/3.0/}). This lab was written for OpenIntro by Andrew Bray and Mine \c{C}etinkaya-Rundel.}

In this lab we'll investigate the probability distribution that is most central to statistics: the normal distribution.  If we are confident that our data are nearly normal, that opens the door to many powerful statistical methods.  Here we'll use the graphical tools of R to assess the normality of our data and also learn how to generate random numbers from a normal distribution.

\subsection*{The Data}
This week we'll be working with measurements of body dimensions.  This data set contains measurements from 247 men and 260 women, most of whom were considered healthy young adults.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{download.file}\hlstd{(}\hlstr{"http://www.openintro.org/stat/data/bdims.RData"}\hlstd{,} \hlkwc{destfile} \hlstd{=} \hlstr{"bdims.RData"}\hlstd{)}

\hlkwd{load}\hlstd{(}\hlstr{"bdims.RData"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}


Let's take a quick peek at the first few rows of the data.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{head}\hlstd{(bdims)}
\end{alltt}
\end{kframe}
\end{knitrout}


You'll see that for every observation we have 25 measurements, many of which are either diameters or girths.  A key to the variable names can be found at \web{http://www.openintro.org/stat/data/bdims.php}, but we'll be focusing on just three columns to get started: weight in kg (\hlstd{wgt}), height in cm (\hlstd{hgt}), and \hlstd{sex} (\hlnum{1} indicates male, \hlnum{0} indicates female).

Since males and females tend to have different body dimensions, it will be useful to create two additional data sets: one with only men and another with only women.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{mdims} \hlkwb{<-} \hlkwd{subset}\hlstd{(bdims, bdims}\hlopt{$}\hlstd{sex} \hlopt{==} \hlnum{1}\hlstd{)}

\hlstd{fdims} \hlkwb{<-} \hlkwd{subset}\hlstd{(bdims, bdims}\hlopt{$}\hlstd{sex} \hlopt{==} \hlnum{0}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}


\begin{exercise}
Make a histogram of men's heights and a histogram of women's heights.  How would you compare the various aspects of the two distributions?
\end{exercise}

\subsection*{The normal distribution}
In your description of the distributions, did you use words like ``bell-shaped'' or ``normal''?  It's tempting to say so when faced with a unimodal symmetric distribution.

To see how accurate that description is, we can plot a normal distribution curve on top of a histogram to see how closely the data follow a normal distribution. This normal curve should have the same mean and standard deviation as the data. We'll be working with women's heights, so let's store them as a separate object and then calculate some statistics that will be referenced later. 

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{fhgtmean} \hlkwb{<-} \hlkwd{mean}\hlstd{(fdims}\hlopt{$}\hlstd{hgt)}

\hlstd{fhgtsd} \hlkwb{<-} \hlkwd{sd}\hlstd{(fdims}\hlopt{$}\hlstd{hgt)}
\end{alltt}
\end{kframe}
\end{knitrout}


Next we make a density histogram to use as the backdrop and use the \hlkwd{lines} function to overlay a normal probability curve. The difference between a frequency histogram and a density histogram is that while in a frequency histogram the \emph{heights} of the bars add up to the total number of observations, in a density histogram the \emph{areas} of the bars add up to 1. The area of each bar can be calculated as simply the height $\times$ the width of the bar. Using a density histogram allows us to properly overlay a normal distribution curve over the histogram since the curve is a normal probability density function. Frequency and density histograms both display the same exact shape; they only differ in their y-axis. You can verify this by comparing the frequency histogram you constructed earlier and the density histogram created by the commands below.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{hist}\hlstd{(fdims}\hlopt{$}\hlstd{hgt,} \hlkwc{probability} \hlstd{=} \hlnum{TRUE}\hlstd{)}

\hlstd{x} \hlkwb{<-} \hlnum{140}\hlopt{:}\hlnum{190}

\hlstd{y} \hlkwb{<-} \hlkwd{dnorm}\hlstd{(}\hlkwc{x} \hlstd{= x,} \hlkwc{mean} \hlstd{= fhgtmean,} \hlkwc{sd} \hlstd{= fhgtsd)}

\hlkwd{lines}\hlstd{(}\hlkwc{x} \hlstd{= x,} \hlkwc{y} \hlstd{= y,} \hlkwc{col} \hlstd{=} \hlstr{"blue"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}


After plotting the density histogram with the first command, we create the x- and y-coordinates for the normal curve.  We chose the \hlstd{x} range as 140 to 190 in order to span the entire range of \hlstd{fheight}.  To create \hlstd{y}, we use \hlkwd{dnorm} to calculate the density of each of those x-values in a distribution that is normal with mean \hlstd{fhgtmean} and standard deviation \hlstd{fhgtsd}.  The final command draws a curve on the existing plot (the density histogram) by connecting each of the points specified by \hlstd{x} and \hlstd{y}. The argument \hlkwc{col} simply sets the color for the line to be drawn. If we left it out, the line would be drawn in black.\symbolfootnote[2]{The top of the curve is cut off because the limits of the x- and y-axes are set to best fit the histogram.  To adjust the y-axis you can add a third argument to the histogram function: \texttt{hist(fdims\$hgt, probability = TRUE,  ylim = c(0, 0.06))}.} 

\begin{exercise}
Based on the this plot, does it appear that the data follow a nearly normal distribution?
\end{exercise}

\subsection*{Evaluating the normal distribution}
Eyeballing the shape of the histogram is one way to determine if the data appear to be nearly normally distributed, but it can be frustrating to decide just how close the histogram is to the curve. An alternative approach involves constructing a normal probability plot, also called a normal Q-Q plot for ``quantile-quantile''.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{qqnorm}\hlstd{(fdims}\hlopt{$}\hlstd{hgt)}

\hlkwd{qqline}\hlstd{(fdims}\hlopt{$}\hlstd{hgt)}
\end{alltt}
\end{kframe}
\end{knitrout}


A data set that is nearly normal will result in a probability plot where the points closely follow the line.  Any deviations from normality leads to deviations of these points from the line.  The plot for female heights shows points that tend to follow the line but with some errant points towards the tails.  We're left with the same problem that we encountered with the histogram above: how close is close enough?

A useful way to address this question is to rephrase it as: what do probability plots look like for data that I \emph{know} came from a normal distribution?  We can answer this by simulating data from a normal distribution using \hlkwd{rnorm}.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{sim_norm} \hlkwb{<-} \hlkwd{rnorm}\hlstd{(}\hlkwc{n} \hlstd{=} \hlkwd{length}\hlstd{(fdims}\hlopt{$}\hlstd{hgt),} \hlkwc{mean} \hlstd{= fhgtmean,} \hlkwc{sd} \hlstd{= fhgtsd)}
\end{alltt}
\end{kframe}
\end{knitrout}


The first argument indicates how many numbers you'd like to generate, which we specify to be the same number of heights in the \hlstd{fdims} data set using the \hlkwd{length} function.  The last two arguments determine the mean and standard deviation of the normal distribution from which the simulated sample will be generated.  We can take a look at the shape of our simulated data set, \hlstd{sim\_norm}, as well as its normal probability plot.

\begin{exercise}
Make a normal probability plot of \hlstd{sim\_norm}.  Do all of the points fall on the line?  How does this plot compare to the probability plot for the real data?
\end{exercise}

Even better than comparing the original plot to a single plot generated from a normal distribution is to compare it to many more plots using the following function. It may be helpful to click the ``zoom" button in the plot window.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{qqnormsim}\hlstd{(fdims}\hlopt{$}\hlstd{hgt)}
\end{alltt}
\end{kframe}
\end{knitrout}


\begin{exercise}
Does the normal probability plot for \hlstd{fdims}\hlopt{\textdollar}\hlstd{hgt} look similar to the plots created for the simulated data?  That is, do plots provide evidence that the female heights are nearly normal?
\end{exercise}

\begin{exercise}
Using the same technique, determine whether or not female weights appear to come from a normal distribution.
\end{exercise}

\subsection*{Normal probabilities}
Okay, so now you have a slew of tools to judge whether or not a variable is normally distributed.  Why should we care?

It turns out that statisticians know a lot about the normal distribution.  Once we decide that a random variable is approximately normal, we can answer all sorts of questions about that variable related to probability.  Take, for example, the question of, ``What is the probability that a randomly chosen young adult female is taller than 6 feet (about 182 cm)?"\symbolfootnote[2]{The study that published this data set is clear to point out that the sample was not random and therefore inference to a general population is not suggested.  We do so here only as an exercise.}

If we assume that female heights are normally distributed (a very close approximation is also okay), we can find this probability by calculating a Z score and consulting a Z table (also called a normal probability table).  In R, this is done in one step with the function \hlkwd{pnorm}.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlnum{1} \hlopt{-} \hlkwd{pnorm}\hlstd{(}\hlkwc{q} \hlstd{=} \hlnum{182}\hlstd{,} \hlkwc{mean} \hlstd{= fhgtmean,} \hlkwc{sd} \hlstd{= fhgtsd)}
\end{alltt}
\end{kframe}
\end{knitrout}


Note that the function \hlkwd{pnorm} gives the area under the normal curve below a given value, \hlkwc{q}, with a given mean and standard deviation.  Since we're interested in the probability that someone is taller than 182 cm, we have to take one minus that probability.

Assuming a normal distribution has allowed us to calculate a theoretical probability.  If we want to calculate the probability empirically, we simply need to determine how many observations fall above 182 then divide this number by the total sample size.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{sum}\hlstd{(fdims}\hlopt{$}\hlstd{hgt} \hlopt{>} \hlnum{182}\hlstd{)}\hlopt{/}\hlkwd{length}\hlstd{(fdims}\hlopt{$}\hlstd{hgt)}
\end{alltt}
\end{kframe}
\end{knitrout}


Although the probabilities are not exactly the same, they are reasonably close. The closer that your distribution is to being normal, the more accurate the theoretical probabilities will be.

\begin{exercise}
Write out two probability questions that you would like to answer; one regarding female heights and one regarding female weights.  Calculate the those probabilities using both the theoretical normal distribution as well as the empirical distribution (four probabilities in all).  Which variable, height or weight, had a closer agreement between the two methods?
\end{exercise}

\vspace{2cm}

\subsection*{On Your Own}

\begin{enumerate}

\item Now let's consider some of the other variables in the body dimensions data set.  Using the figures on the next page, match the histogram to its normal probability plot.  All of the variables have been standardized (first subtract the mean, then divide by the standard deviation), so the units won't be of any help.  If you are uncertain based on these figures, generate the plots in R to check.

\begin{enumerate}[(a)]

\item The histogram for female biiliac (pelvic) diameter (\hlstd{bii.di}) belongs to normal probability plot letter \rule{0.25in}{1pt}.

\item The histogram for female elbow diameter (\hlstd{elb.di}) belongs to normal probability plot letter \rule{0.25in}{1pt}.

\item The histogram for general age (\hlstd{age}) belongs to normal probability plot letter \rule{0.25in}{1pt}.

\item The histogram for female chest depth (\hlstd{che.de}) belongs to normal probability plot letter \rule{0.25in}{1pt}.

\end{enumerate}

\item Note that normal probability plots C and D have a slight stepwise pattern.  Why do you think this is the case?

\item As you can see, normal probability plots can be used both to assess normality and visualize skewness.  Make a normal probability plot for female knee diameter (\hlstd{kne.di}).  Based on this normal probability plot, is this variable left skewed, symmetric, or right skewed?  Use a histogram to confirm your findings.

\item What concepts from the textbook are covered in this lab?  What concepts, if any, are not covered in the textbook?  Have you seen these concepts elsewhere, e.g. lecture, discussion section, previous labs, or homework problems?  Be specific in your answer.

\end{enumerate}

\newpage

\begin{figure}[h]
   \centering
  \includegraphics[width=6.25 in]{more/histQQmatch.pdf}
\end{figure}

\end{document}
